#include"Runqueue.hpp"
#include"Sem.hpp"

struct threaddata
{
    RunQueue<int>* _rq;
    std::string name;
};

int data=1;
void* consumer(void* args)
{
    //消费者
    threaddata* td=static_cast<threaddata*>(args);
    while(1)
    {
        sleep(2);
        int n=0;
        int* out=&n;
        td->_rq->Pop(out);
        std::cout<<"线程"<<td->name<<"消费了一个数据："<<*out<<std::endl;
    }
}

void* produce(void* args)
{
    //生产者：
   
    threaddata* td=static_cast<threaddata*>(args);
    while(1)
    {
        td->_rq->Equeue(data);
        std::cout<<"线程"<<td->name<<"生产了一个数据："<<data<<std::endl;
        data++;
    }
}

int main()
{
    RunQueue<int>* rq=new RunQueue<int>();

    //创建两个线程充当生产者
    //创建三个线程充当消费者
    pthread_t c[3],p[2];
    threaddata* td1=new threaddata;
    td1->name="thread-produce-1";
    td1->_rq=rq;
    threaddata* td2=new threaddata;
    td2->name="thread-produce-2";
    td2->_rq=rq;
    threaddata* td3=new threaddata;
    td3->name="thread-consumer-3";
    td3->_rq=rq;
    threaddata* td4=new threaddata;
    td4->name="thread-consumer-4";
    td4->_rq=rq;
    threaddata* td5=new threaddata;
    td5->name="thread-consumer-5";
    td5->_rq=rq;

    pthread_create(p, nullptr, produce,td1);
    pthread_create(p+1, nullptr, produce,td2);
    pthread_create(c, nullptr, consumer,td3);
    pthread_create(c+1, nullptr, consumer,td4);
    pthread_create(c+2, nullptr, consumer,td5);



    pthread_join(*p,nullptr);
    pthread_join(*(p+1),nullptr);
    pthread_join(*c,nullptr);
    pthread_join(*(c+1),nullptr);
    pthread_join(*(c+2),nullptr);

    std::cout<<"over"<<std::endl;
    
    return 0;
}